package com.gl.saasservice.sys.controller;

import com.gl.framework.web.response.Result;
import com.gl.saasservice.sys.service.PermissionService;
import com.gl.saasservice.sys.vo.PermissionVO;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping(path = "/permission")
public class PermissionController {
    private static final Logger LOG = LoggerFactory.getLogger(PermissionController.class);

    @Autowired
    private PermissionService permissionService;

    /**
     * 权限列表
     * @param token
     * @return
     */
    @RequestMapping(value = "/pages", method = RequestMethod.GET)
    public Result page(@RequestHeader String token,
                               @RequestParam(name = "parentId", required = false) Long parentId,
                               @RequestParam(name = "filter", required = false) String filter) {

        Map<String,Object> dataMap =new HashMap<>();
        try {
            dataMap = permissionService.pages(token,parentId,filter);
        } catch (Exception e) {
            LOG.error(e.getMessage(), e);
            Result.fail();
        }
        return Result.success(dataMap.get("data"));
    }

    /**
     * 当前用户有权限的菜单列表
     * @param token
     * @return
     */
/*    @RequestMapping(value = "/pagesByUser", method = RequestMethod.GET)
    public ResultBean<Object> pageByUser(@RequestHeader String accessToken) {
        ResultBean<Object> result = new ResultBean<>();
        try {
            List<PermissionVO> list=permissionService.findPermissionByUserSortby(accessToken);
            Map<String, Object> dataMap  =permissionService.dealwithResult(list);
            result.setCode(StatusCode.SUCCESS_CODE_10000.getErrorCode());
            result.setResultData(dataMap);
        } catch (Exception e) {
            LOG.error(e.getMessage(), e);

            result.setCode(StatusCode.ERROR_CODE_10001.getErrorCode());
            result.setMsg(e.getMessage());
        }
        return result;
    }*/

    /**
     * 添加权限信息
     * @param token
     * @param permissionVO
     * @return
     */
    @RequestMapping(method = RequestMethod.POST)
    public Result add (@RequestHeader String token,
                                   @RequestBody PermissionVO permissionVO){
        return permissionService.save(token,permissionVO);
    }

    /**
     * 更新权限信息
     * @param token
     * @param permissionVO
     * @return
     */
    @RequestMapping(method = RequestMethod.PUT)
    public Result update (@RequestHeader String token,
                                   @RequestBody PermissionVO permissionVO){
        return permissionService.save(token,permissionVO);
    }

    /**
     * 删除权限信息
     * @param token
     * @param id
     * @return
     */
    @RequestMapping(value = "{id}",method = RequestMethod.DELETE)
    public Result delete(@RequestHeader String token,
                                     @PathVariable Long id){
        return permissionService.delete(token,id);
    }


}
